/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _GD25Q_HAL_MODEL_
#include "sys.h"
#include "stm32f4xx_ll_spi.h"
#include "gd25q_hal.h"
#include "gd25q_cmd.h"

#define GDSPI SPI1
#define ISR_IRQn SPI1_IRQn

#define PDMA_CH_SPI_RX 11
#define PDMA_CH_SPI_TX 10

uint32_t DataFixed;

void gdmaRxIsr(void);
void gdmaTxIsr(void);

void FlashQ25ReadDmaCallBack(void);

/*****************************************************************************/ /*!
* @brief   hal init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_init()
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    LL_SPI_InitTypeDef SPI_InitStruct = {0};
      
    //cs
    INTX_DISABLE();
    
    __HAL_RCC_SPI1_CLK_ENABLE();
    __HAL_RCC_SPI1_FORCE_RESET();
    __HAL_RCC_SPI1_RELEASE_RESET();


    GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = 0;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    SPI_CS = 1;
    
    INTX_ENABLE();

    /* SPI1 parameter config */
    SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
    SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
    SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
    SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
    SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
    SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
    SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;
    SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
    
    SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
    SPI_InitStruct.CRCPoly = 1;
    LL_SPI_Init(GDSPI, &SPI_InitStruct);

    LL_SPI_Enable(GDSPI);

    gd25_cs(0);
    gd25_spi_rw(0xFF);
    gd25_cs(1);
}

/*****************************************************************************/ /*!
* @brief   spi data read and write.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t gd25_spi_rw(uint8_t dataW)
{

    volatile uint8_t dataR;

    while (GDSPI->SR & SPI_SR_RXNE)
        dataR = GDSPI->DR;

    GDSPI->DR = (uint32_t)dataW;
    
    NOP8();

    while (!(GDSPI->SR & SPI_SR_RXNE));

    dataR = GDSPI->DR;

    return (dataR);
}

uint8_t gd25_spi_read(uint8_t *datar, uint32_t Len)
{

    volatile register uint8_t dataR;

    while (GDSPI->SR & SPI_SR_RXNE)
        dataR = GDSPI->DR;

    while (Len--)
    {
        GDSPI->DR = 0xFF;
        
        NOP8();
        
        while (!(GDSPI->SR & SPI_SR_RXNE));
        
        *datar++ = GDSPI->DR;
    }
    return (dataR);
}

uint8_t gd25_spi_write(uint8_t *dataw, uint32_t Len)
{
    volatile register uint8_t dataR;

    while (GDSPI->SR & SPI_SR_RXNE)
        dataR = GDSPI->DR;

    while (Len--)
    {
        GDSPI->DR = *dataw++;
        
        NOP8();
        
        while (!(GDSPI->SR & SPI_SR_RXNE));
        
        dataR = GDSPI->DR;
    }

    return (dataR);
}

/*****************************************************************************/ /*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_dma_read(uint8_t *Data, uint32_t Len)
{

}

/*****************************************************************************/ /*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_dma_write(uint8_t *Data, uint32_t Len)
{

}

/*****************************************************************************/ /*!
* @brief   RX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gdmaRxIsr()
{


}

/*****************************************************************************/ /*!
* @brief   TX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gdmaTxIsr()
{
}
